<?php 

class CommentController extends ApplicationController {
	protected $AppName = "comment";
	protected $IncludeFiles = array("comment.class.php", "comment.group.class.php");
	public $UseApps = array(array("name" => "mysql"),
					  array("name" => "account"),
					  array("name" => "paginator"),
					  array("name" => "smarty"));
	
	function init(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		
		$HTTP = new HTTPRequest();
		
		$URLParameter = $HTTP->get($URLPosition);
		
		switch ($URLParameter){
			
			// AJAX FUNCTION
			case "add-comment":
				$App = $_POST["App"];
				$AppEntry = $_POST["AppEntry"];
				$Author = $_POST["Author"];
				$AuthorEmail = $_POST["AuthorEmail"];
				$AuthorWebSite = $_POST["AuthorWebSite"];
				$CommentContent = $_POST["CommentContent"];
				
				$this->createComment($App, $AppEntry, $CommentContent, $Author, $AuthorEmail, $AuthorWebSite);
				
				return (string)mysql_insert_id();
			break;
			
			// AJAX FUNCTION
			case "get-comment":
				$CommentID = $HTTP->get($URLPosition+1);
				
				if(!is_numeric($CommentID)){
					return $this->Language->get("id_not_numeric");
				}
				
				$Comment = $this->getComment($CommentID);
				
				$SmartyC = $this->UseApps["smarty"];
				$Smarty = $SmartyC->getSmarty();
				
				$Smarty->assign("Comment", $Comment);
				
				$this->ViewFile = "comment_template.tpl";
				
				return true;
			break;
		}
		
		return $this;
	}
	
	function createComment($app, $app_entry, $comment_content, $author, $author_email, $author_website = null){
		$MySQL = $this->UseApps["mysql"];
		$AccountController = $this->UseApps["account"];
		
		// Set data
		$CommentContent = escape($comment_content, $MySQL);
		$CommentAuthor = $author;
		
		if($AccountController->isLogged()){
			$CommentAuthorMember = true;
		}
		else {
			$CommentAuthorMember = false;
		}
		
		if(!isEmail($author_email)){
			return false;
		}
		
		$CommentAuthorEmail = $author_email;
		$CommentAuthorWebsite = escape($author_website, $MySQL);
		$CommentDateTime = time();
		
		$MySQL->query("INSERT INTO {$MySQL->getPrefix()}comment (author, author_member, author_email, author_website, application_owner, application_entry_id, comment_content, date_time) VALUES('$CommentAuthor', '$CommentAuthorMember', '$CommentAuthorEmail', '$CommentAuthorWebsite', '$app', $app_entry, '$CommentContent', '$CommentDateTime')");
		
		return true;
	}
	
	function getComment($id){
		$Comment = new Comment($id, $this);
		
		return $Comment;
	}
	
	function getComments($params = array()){
		$App = $params[0];
		$AppEntryID = $params[1];
		
		$CommentGroup = new CommentGroup($App, $AppEntryID, $this);
		
		$AccountC = $this->UseApps["account"];
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$SmartyC->addHeadTag("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/comment/views/comment.css\" />");
		$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/comment/comment.js\"></script>");
		
		$Smarty->assign("CommentGroup", $CommentGroup);
		$Smarty->assign("IsLogged", $AccountC->isLogged());
		$Smarty->assign("UserAccount", $AccountC->getAccount());
		
		$this->ViewFile = "show_comment.tpl";
		
		return true;
	}
}

?>